import Vue from 'vue'
import store from './vuex/index.js'
import App from './App.vue'
import fastClick from 'fastclick'
import router from './router/index.js'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'mint-ui/lib/style.css';
import Mint from 'mint-ui';
//import 'src/assets/js/iconfont.js'
import './componentes/index.js' // 23KB
import './utils/base64.js'; // 11KB
import './utils/filters.js'; // 11KB
import {getCode} from 'src/utils/getCode.js'
import {Common} from 'src/utils/common.js'
import {Storage} from 'src/utils/storage.js'

Vue.use(Mint);
Vue.use(VueAwesomeSwiper);

window.isScroll = false // 设置是否页面进行滚动
window.isBack = false; // 是否点击后退按钮
if ('addEventListener' in document) {
  document.addEventListener('DOMContentLoaded', function () { //写入全局监听事件
    fastClick.attach(document.body);
    document.addEventListener('mousewheel', e => {
      if (window.isScroll) {
        e.preventDefault() // 禁止滚动
      }
    }, false)
    document.addEventListener('touchmove', e => {
      if (window.isScroll) {
        e.preventDefault() // 禁止滚动
      }
    }, false)
    window.addEventListener('hashchange', (e) => {
      const routerPath = Storage.get('routerPath', true) // 赋值路由去向
      if (e.oldURL.split("#")[1]) {
        window.isBack = e.oldURL.split("#")[1].indexOf(routerPath.goPath) >= 0
      }
    }, false)
  }, false);
}

/**
 *公共beforeRoute
 */
if (!Storage.get('routerPath', true)) {
  Storage.set('routerPath', {goPath: "", fromPath: ""}, true) // 存储路由去向初始化
}
router.beforeEach(async (to, from, next) => {
  const routerPath = Storage.get('routerPath', true) // 赋值上一次的路由去向
  if (routerPath.goPath === to.path && !sessionStorage.getItem("jumpPage")) { // 刷新页面重新签名
    Common.getSign(true) // 微信签名,true表示设置微信邀请分享链接。
  }
  window.isBack = false // 保证isBack为false值
  let code = Tools.getQueryValue("code"); // 获取微信code
  let loginState = Storage.get("isLogin", true); // 登录状态
  if (!loginState) {
    // 未登录
    if (code) {
      Storage.set('routerPath', {goPath: to.path, fromPath: from.path}, true) // 存储本次路由跳转页面
      Common.getSign() // 获取签名
      next()
    } else {
      if (sessionStorage.getItem('jumpPage')) {
        // 外部链接进入判断
        Common.getSign() // 获取签名
        next();
      } else {
        getCode();
        process.env.NODE_ENV === 'production' ? "" : next(); // 本地启动需要next()，服务器不进行跳转
      }
    }
  } else {
    Storage.set('routerPath', {goPath: to.path, fromPath: from.path}, true) // 存储本次路由跳转页面
    // 已登录
    next();
  }
});
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: {
    App
  }
});
